package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class WaterfallAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<ListItem> listItemList;

    public WaterfallAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        ListItem listItem = this.listItemList.get(position);
        itemViewHolder.title.setText(listItem.getTitle());
        itemViewHolder.summary.setText(listItem.getSummary());
        itemViewHolder.itemView.setBackgroundColor(listItem.getColor());
        int height = 0;
        switch (position % 4) {
            case 0:
            case 2: {
                height = 800;
            }
            break;
            case 1:
            case 3: {
                height = 500;
            }
            break;
        }
        ViewGroup.LayoutParams layoutParams = itemViewHolder.itemView.getLayoutParams();
        layoutParams.height = height;
        itemViewHolder.itemView.setLayoutParams(layoutParams);
    }

    @Override
    public int getItemViewType(int position) {
        return listItemList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return this.listItemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ItemViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }
}
